// 蜂窝蜜造平台生成代码，如手工更改，请添加到 .beeignore 忽略生成
package com.fowo.api.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fowo.api.common.model.IdListPo;
import com.fowo.api.common.model.IdPo;
import com.fowo.api.common.model.ImportRow;
import com.fowo.api.common.model.OptionItem;
import com.fowo.api.common.model.R;
import com.fowo.api.common.util.ResponseUtils;
import com.fowo.api.entity.StockOut;
import com.fowo.api.model.stock.out.StockOutImportPo;
import com.fowo.api.model.stock.out.StockOutItemVo;
import com.fowo.api.model.stock.out.StockOutSearchParamPo;
import com.fowo.api.model.stock.out.StockOutVo;
import com.fowo.api.service.StockOutService;
import com.fowo.api.sys.entity.SysFile;
import com.fowo.api.user.model.JwtUserInfo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import org.springframework.core.io.ClassPathResource;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.StreamUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

@Tag(name = "出库单")
@RequestMapping("/api/stock-out")
@RestController
public class StockOutController {

  @Resource
  private StockOutService service;

  @Operation(summary = "查询结果")
  @GetMapping
  public R<List<StockOutItemVo>> search(StockOutSearchParamPo search) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(service.search(search));
  }

  @Operation(summary = "快速查询选项(有缓存)")
  @GetMapping("options")
  public R<List<OptionItem<StockOutItemVo>>> searchOptions(
    StockOutSearchParamPo search
  ) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(service.searchOptions(search));
  }

  @Operation(summary = "查询最后更新时间")
  @GetMapping("lastUpdateTime")
  public R<Date> searchLastUpdateTime(StockOutSearchParamPo search) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(service.searchLastUpdateTime(search));
  }

  @Operation(summary = "获取满足搜索条件的统计信息")
  @GetMapping("statistical")
  public R<?> searchForStatistical(StockOutSearchParamPo search) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(service.searchForStatistical(search));
  }

  @Operation(summary = "获取分页查询结果")
  @GetMapping("page")
  public R<Page<StockOutItemVo>> getPage(StockOutSearchParamPo search) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(service.pageSearch(search));
  }

  @Operation(summary = "获取包括子表的查询结果")
  @GetMapping("page-with-child")
  public R<Page<?>> getPageWithChild(StockOutSearchParamPo search) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(service.pageWithChildSearch(search));
  }

  @Operation(summary = "仅获取子表的查询结果")
  @GetMapping("page-only-child/{parentId}")
  public R<Page<?>> getPageOnlyChild(
    @PathVariable Long parentId,
    StockOutSearchParamPo search
  ) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(service.pageOnlyChildSearch(parentId, search));
  }

  @Operation(summary = "查询是否后台下载")
  @GetMapping("getBackgroundExportFlag")
  public R<Boolean> getBackgroundExportFlag(StockOutSearchParamPo search) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(service.getBackgroundExportFlag(search));
  }

  @Operation(summary = "获取详情")
  @GetMapping("{id}")
  public R<StockOutVo> getVoById(@PathVariable Long id) {
    return R.data(() -> service.getVoById(id));
  }

  @Operation(summary = "获取出库单号相关的编号列表")
  @GetMapping("order-sn/{texts}/ids")
  public R<List<Long>> getIdsByOrderSn(@PathVariable String texts) {
    List<String> nameList = Arrays
      .stream(texts.split(","))
      .collect(Collectors.toList());
    return R.data(() -> service.getIdsByOrderSn(nameList));
  }

  @Operation(summary = "获取编号相关的出库单号列表")
  @GetMapping("order-sn/{ids}")
  public R<List<String>> getVoById(@PathVariable String ids) {
    List<Long> idList = Arrays
      .asList(ids.split(","))
      .stream()
      .map(Long::valueOf)
      .collect(Collectors.toList());
    return R.data(() -> service.getOrderSnByIds(idList));
  }

  @PreAuthorize("@am.canAccess('stockOutCreate')")
  @Operation(summary = "新增")
  @PostMapping
  public R<Long> create(@Validated @RequestBody StockOutVo model) {
    return R.data(() -> service.create(model));
  }

  @PreAuthorize("@am.canAccess('stockOutEdit')")
  @Operation(summary = "更新")
  @PutMapping
  public R update(@RequestBody StockOutVo model) {
    return R.run(() -> service.update(model));
  }

  @PreAuthorize("@am.canAccess('stockOutEdit')")
  @Operation(summary = "编辑更新")
  @PutMapping("edit")
  public R updateForEdit(@RequestBody StockOutVo model) {
    return R.run(() -> service.updateForEdit(model));
  }

  @PreAuthorize("@am.canAccess('stockOutDelete')")
  @Operation(summary = "删除")
  @DeleteMapping("{id}")
  public R delete(@PathVariable Long id) {
    return R.run(() -> service.delete(id));
  }

  @PreAuthorize("@am.canAccess('stockOutDelete')")
  @Operation(summary = "批量删除")
  @DeleteMapping("batch")
  public R batchDelete(@RequestBody IdListPo po) {
    return R.run(() -> service.batchDelete(po.getIds()));
  }

  @PreAuthorize("@am.canAccess('stockOutImport')")
  @Operation(summary = "导入预览")
  @PostMapping("import-preview")
  public R<List<ImportRow<StockOutImportPo>>> importPreview(
    @RequestBody SysFile sysFile,
    @RequestParam(name = "template", required = false) Long templateId,
    @RequestParam(
      name = "allowOverrides",
      defaultValue = "false"
    ) boolean allowOverrides
  ) {
    return R.data(() ->
      service.importPreview(sysFile, templateId, allowOverrides)
    );
  }

  @PreAuthorize("@am.canAccess('stockOutImport')")
  @Operation(summary = "完成导入")
  @PostMapping("import-done")
  public R importDone(
    @RequestBody List<ImportRow<StockOutImportPo>> rows,
    @RequestParam(name = "template", required = false) Long templateId
  ) {
    return R.run(() -> service.importDone(rows, templateId));
  }

  @Operation(summary = "下载导入模版")
  @GetMapping("import/template")
  public void downloadImportTemplate(HttpServletResponse response)
    throws IOException {
    ResponseUtils.setAttachmentFileName(response, "出库单导入模版.xlsx");
    InputStream inputStream = new ClassPathResource("templates/StockOut.xlsx")
      .getInputStream();
    StreamUtils.copy(inputStream, response.getOutputStream());
  }

  @Operation(summary = "导出")
  @GetMapping("export")
  @PreAuthorize("@am.canAccess('stockOutExport')")
  public void export(
    @RequestParam(name = "template", required = false) Long templateId,
    StockOutSearchParamPo search,
    HttpServletResponse response
  ) throws Exception {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    service.export(templateId, search, response);
  }

  @PreAuthorize("@am.canAccess('stockOutConfirmationOfDeliverys')")
  @Operation(summary = "确认出库")
  @PostMapping("confirmationOfDeliverys")
  public R confirmationOfDeliverys(@RequestBody IdPo idPo) {
    return R.run(() -> service.confirmationOfDeliverys(idPo.getId()));
  }

  @PreAuthorize("@am.canAccess('stockOutAnnul')")
  @Operation(summary = "撤销")
  @PostMapping("annul")
  public R annul(@RequestBody IdPo idPo) {
    return R.run(() -> service.annul(idPo.getId()));
  }

  @PreAuthorize("@am.canAccess('stockOutRefer')")
  @Operation(summary = "提交")
  @PostMapping("refer")
  public R refer(@RequestBody IdListPo po) {
    return R.run(() -> service.refer(po.getIds()));
  }

  @PreAuthorize("@am.canAccess('stockOutConfirmOutboundBatch')")
  @Operation(summary = "确认出库")
  @PostMapping("confirmOutboundBatch")
  public R confirmOutboundBatch(@RequestBody IdListPo po) {
    return R.run(() -> service.confirmOutboundBatch(po.getIds()));
  }

  @PreAuthorize("@am.canAccess('stockOutBatchPushLingXing')")
  @Operation(summary = "出库单批量推送领星")
  @PostMapping("batchPushLingXing")
  public R batchPushLingXing(@RequestBody IdListPo po) {
    return R.run(() -> service.batchPushLingXing(po.getIds()));
  }

  @PreAuthorize("@am.canAccess('stockOutOutpush')")
  @Operation(summary = "出库并推送领星")
  @PostMapping("outpush")
  public R outpush(@RequestBody IdListPo po) {
    return R.run(() -> service.outpush(po.getIds()));
  }

  @PreAuthorize("@am.canAccess('stockOutChukujianqusuoding')")
  @Operation(summary = "出库减去锁定的")
  @PostMapping("chukujianqusuoding")
  public R chukujianqusuoding(@RequestBody IdListPo idListPo) {
    return R.run(() -> service.chukujianqusuoding(idListPo.getIds()));
  }

  @PreAuthorize("@am.canAccess('stockOutOmit')")
  @Operation(summary = "删除")
  @PostMapping("omit")
  public R omit(@RequestBody IdListPo po) {
    return R.run(() -> service.omit(po.getIds()));
  }

  @PreAuthorize("@am.canAccess('stockOutPushLingXing')")
  @Operation(summary = "出库单推送领星")
  @PostMapping("pushLingXing")
  public R pushLingXing(@RequestBody IdPo po) {
    return R.run(() -> service.pushLingXing(po.getId()));
  }

  @PreAuthorize("@am.canAccess('stockOutOutboundReject')")
  @Operation(summary = "出库扣减次品")
  @PostMapping("outboundReject")
  public R outboundReject(@RequestBody IdPo idPo) {
    return R.run(() -> service.outboundReject(idPo.getId()));
  }

  @PreAuthorize("@am.canAccess('stockOutOutboundAvailable')")
  @Operation(summary = "出库扣减可用")
  @PostMapping("outboundAvailable")
  public R outboundAvailable(@RequestBody IdPo idPo) {
    return R.run(() -> service.outboundAvailable(idPo.getId()));
  }

  @PreAuthorize("@am.canAccess('stockOutCustSubmit')")
  @Operation(summary = "提交")
  @PostMapping("custSubmit")
  public R custSubmit(@RequestBody IdPo po) {
    return R.run(() -> service.custSubmit(po.getId()));
  }

  @PreAuthorize("@am.canAccess('stockOutPushStockOut')")
  @Operation(summary = "下推其他入库单")
  @PostMapping("pushStockOut")
  public R pushStockOut(@RequestBody IdPo po) {
    return R.run(() -> service.pushStockOut(po.getId()));
  }

  @PreAuthorize("@am.canAccess('stockOutPushStockInAndStockOut')")
  @Operation(summary = "下推生成入库单和出库单(工厂)")
  @PostMapping("pushStockInAndStockOut")
  public R pushStockInAndStockOut(@RequestBody IdPo po) {
    return R.run(() -> service.pushStockInAndStockOut(po.getId()));
  }

  @PreAuthorize("@am.canAccess('stockOutCreateReceivable')")
  @Operation(summary = "生成应收单")
  @PostMapping("createReceivable")
  public R createReceivable(@RequestBody IdPo po) {
    return R.run(() -> service.createReceivable(po.getId()));
  }

  @PreAuthorize("@am.canAccess('stockOutCancelOutbound')")
  @Operation(summary = "取消审核")
  @PostMapping("cancelOutbound")
  public R cancelOutbound(@RequestBody IdPo idPo) {
    return R.run(() -> service.cancelOutbound(idPo.getId()));
  }

  /**
   * 视图选项卡标题上显示计数
   */
  @Operation(summary = "视图选项卡标题上显示计数")
  @GetMapping("getTabHeaderShowTotal")
  public R<Map> getTabHeaderShowTotal() {
    return R.data(() -> service.getTabHeaderShowTotal());
  }

  @Operation(summary = "全部")
  @GetMapping("all-1")
  public R<List<StockOutItemVo>> all1(StockOutSearchParamPo search) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(() -> service.all1(search));
  }

  @Operation(summary = "全部分页")
  @GetMapping("all-1Page")
  public R<Page<StockOutItemVo>> all1Page(StockOutSearchParamPo search) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(() -> service.all1Page(search));
  }

  @Operation(summary = "全部包括子表的查询结果")
  @GetMapping("all-1Page-with-child")
  public R<Page<?>> all1PageWithChild(StockOutSearchParamPo search) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(service.all1PageWithChildSearch(search));
  }

  @Operation(summary = "全部最后更新时间")
  @GetMapping("all-1LastUpdateTime")
  public R<Date> all1LastUpdateTime(StockOutSearchParamPo search) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(() -> service.all1LastUpdateTime(search));
  }

  @Operation(summary = "待提交")
  @GetMapping("changes-to-be-committed")
  public R<List<StockOutItemVo>> changesToBeCommitted(
    StockOutSearchParamPo search
  ) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(() -> service.changesToBeCommitted(search));
  }

  @Operation(summary = "待提交分页")
  @GetMapping("changes-to-be-committedPage")
  public R<Page<StockOutItemVo>> changesToBeCommittedPage(
    StockOutSearchParamPo search
  ) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(() -> service.changesToBeCommittedPage(search));
  }

  @Operation(summary = "待提交包括子表的查询结果")
  @GetMapping("changes-to-be-committedPage-with-child")
  public R<Page<?>> changesToBeCommittedPageWithChild(
    StockOutSearchParamPo search
  ) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(service.changesToBeCommittedPageWithChildSearch(search));
  }

  @Operation(summary = "待提交最后更新时间")
  @GetMapping("changes-to-be-committedLastUpdateTime")
  public R<Date> changesToBeCommittedLastUpdateTime(
    StockOutSearchParamPo search
  ) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(() -> service.changesToBeCommittedLastUpdateTime(search));
  }

  @Operation(summary = "待出库")
  @GetMapping("waiting-for-delivery")
  public R<List<StockOutItemVo>> waitingForDelivery(
    StockOutSearchParamPo search
  ) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(() -> service.waitingForDelivery(search));
  }

  @Operation(summary = "待出库分页")
  @GetMapping("waiting-for-deliveryPage")
  public R<Page<StockOutItemVo>> waitingForDeliveryPage(
    StockOutSearchParamPo search
  ) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(() -> service.waitingForDeliveryPage(search));
  }

  @Operation(summary = "待出库包括子表的查询结果")
  @GetMapping("waiting-for-deliveryPage-with-child")
  public R<Page<?>> waitingForDeliveryPageWithChild(
    StockOutSearchParamPo search
  ) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(service.waitingForDeliveryPageWithChildSearch(search));
  }

  @Operation(summary = "待出库最后更新时间")
  @GetMapping("waiting-for-deliveryLastUpdateTime")
  public R<Date> waitingForDeliveryLastUpdateTime(
    StockOutSearchParamPo search
  ) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(() -> service.waitingForDeliveryLastUpdateTime(search));
  }

  @Operation(summary = "已完成")
  @GetMapping("done")
  public R<List<StockOutItemVo>> done(StockOutSearchParamPo search) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(() -> service.done(search));
  }

  @Operation(summary = "已完成分页")
  @GetMapping("donePage")
  public R<Page<StockOutItemVo>> donePage(StockOutSearchParamPo search) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(() -> service.donePage(search));
  }

  @Operation(summary = "已完成包括子表的查询结果")
  @GetMapping("donePage-with-child")
  public R<Page<?>> donePageWithChild(StockOutSearchParamPo search) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(service.donePageWithChildSearch(search));
  }

  @Operation(summary = "已完成最后更新时间")
  @GetMapping("doneLastUpdateTime")
  public R<Date> doneLastUpdateTime(StockOutSearchParamPo search) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(() -> service.doneLastUpdateTime(search));
  }

  @Operation(summary = "已撤销")
  @GetMapping("undone")
  public R<List<StockOutItemVo>> undone(StockOutSearchParamPo search) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(() -> service.undone(search));
  }

  @Operation(summary = "已撤销分页")
  @GetMapping("undonePage")
  public R<Page<StockOutItemVo>> undonePage(StockOutSearchParamPo search) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(() -> service.undonePage(search));
  }

  @Operation(summary = "已撤销包括子表的查询结果")
  @GetMapping("undonePage-with-child")
  public R<Page<?>> undonePageWithChild(StockOutSearchParamPo search) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(service.undonePageWithChildSearch(search));
  }

  @Operation(summary = "已撤销最后更新时间")
  @GetMapping("undoneLastUpdateTime")
  public R<Date> undoneLastUpdateTime(StockOutSearchParamPo search) {
    JwtUserInfo jwtUserInfo = JwtUserInfo.fromHeader();
    if (jwtUserInfo != null) {
      search.setCurrentUserId(jwtUserInfo.getUserId());
    }
    return R.data(() -> service.undoneLastUpdateTime(search));
  }
}
